package com.teachoa.bean;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.teachoa.constant.Global;
import com.teachoa.util.IDUtil;
import com.teachoa.util.Lists;
import com.teachoa.util.Maps;

@SuppressWarnings({"rawtypes", "unchecked"})
public class Dict extends Model<Dict> {
	
	/**
	 * [
            {
              text: "Parent 1",
              nodes: [
                {
                  text: "Child 1",
                  nodes: [
                    {
                      text: "Grandchild 1"
                    },
                    {
                      text: "Grandchild 2"
                    }
                  ]
                },
                {
                	id:"2",
                	pid:"1",
                  text: "Child 2"
                }
              ]
            },
            {
              text: "Parent 2"
            },
            {
              text: "Parent 3"
            },
            {
              text: "Parent 4"
            },
            {
              text: "Parent 5"
            }
          ];
	 * 
	 */

	/**
	 * 
	 */
	private static final long serialVersionUID = -8476461395398190746L;
	
	public static final Dict dao = new Dict();
	
	public List getDictTree() {
		List<Record> list = Db.find("select id, pid as pId, name from dict");
		Record r = new Record();
		r.set("id", null);
		r.set("pId", null);
		r.set("name", "根节点");
		list.add(r);
		return list;
	}
	
	public Page<Dict> findDictByPage(int pageNo, String pid) {
		String suffix = "from dict";
		List list = Lists.newArrayList();
		if(StringUtils.isEmpty(pid)) {
			suffix += " where pid is null";
		} else if(!pid.equals("0")) {
			suffix += " where pid = ?";
			list.add(pid);
		}
		return dao.paginate(pageNo, Global.pageSize, "select * ", suffix, list.toArray());
	}
	
	public List<Dict> getSubDictsByPath(String path) {
		Dict dict = getDictByPath(path);
		return dao.find("select * from dict where pid = ?", dict.getStr("id"));
	}
	
	public Dict getDictByPath(String path) {
		return dao.findFirst("select * from dict where path = ?", path);
	}
	
	/*private void recursion(List<Dict> dicts, List list) {
		for(Dict dict:dicts) {
			int isLeaf = dict.getInt("is_leaf");
			String id = dict.getStr("id");
			String name = dict.getStr("name");
			String pid = dict.getStr("pid");
			Map node = Maps.newHashMap("id", id, "text", name, "pid", pid, "is_leaf", isLeaf);
			if(isLeaf==0) {
				List<Dict> dictList = dao.find("select * from dict where pid = ?", new Object[]{id});
				List nodes = Lists.newArrayList();
				recursion(dictList, nodes);
				node.put("nodes", nodes);
			}
			list.add(node);
		}
	}*/
	
	public void createDict() {
		String root1 = IDUtil.getId();
		dao.setAttrs(Maps.newHashMap("id", root1, "name", "基础数据", "code", "base", "path", "/base"));
		dao.save();
		
		String sex = IDUtil.getId();
		dao.setAttrs(Maps.newHashMap("id", sex, "name", "性别", "code", "sex", "pid", root1, "path", "/base/sex"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "男", "code", "male", "pid", sex, "path", "/base/sex/male", "value", "1"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "女", "code", "female", "pid", sex, "path", "/base/sex/female", "value", "2"));
		dao.save();
		
		String userType = IDUtil.getId();
		dao.setAttrs(Maps.newHashMap("id", userType, "name", "用户类型", "code", "usertype", "pid", root1, "path", "/base/usertype"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "老师", "code", "teacher", "pid", userType, "path", "/base/usertype/teacher", "value", "1"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "学生", "code", "student", "pid", userType, "path", "/base/usertype/student", "value", "2"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "专家", "code", "expert", "pid", userType, "path", "/base/usertype/expert", "value", "2"));
		dao.save();
		
		String root2 = IDUtil.getId();
		dao.setAttrs(Maps.newHashMap("id", root2, "name", "业务数据", "code", "base", "path", "/business"));
		dao.save();
		
		String proStatus = IDUtil.getId();
		dao.setAttrs(Maps.newHashMap("id", proStatus, "name", "项目状态", "code", "prostatus", "pid", root2, "path", "/business/prostatus"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "已开始", "code", "begin", "pid", proStatus, "path", "/business/prostatus/begin", "value", "1"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "name", "已结束", "code", "end", "pid", proStatus, "path", "/business/prostatus/end", "value", "2"));
		dao.save();
	}

}
